package Image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.Random;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.01.24.21.15
 */
public class Ex4 {
    public static void main(String[] args) throws IOException {
        //logo png 格式（画布透明）

        //var logo = ImageIO.read(new File("logo.png"));
        var logo = ImageIO.read(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("fonts/logo.png")));

        var logow = logo.getWidth();
        var logoh = logo.getHeight();

        //src pok.png
        var src = ImageIO.read(new File("pok.jpg"));
        var srcw = src.getWidth();
        var srch = src.getHeight();
        var g = src.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);


        int m = 10;
        //左上角
        int x = 0 + m;
        int y = 0 + m;

        // 右下角
        x = srcw - logow - m;
        y = srch - logoh - m;

        g.setColor(Color.YELLOW);
        g.drawLine(0, srch / 2, srcw, srch / 2);
        g.drawLine(srcw / 2, 0, srcw / 2, srch);

        //正中央
        x = (srcw - logow) / 2;
        y = (srch - logoh) / 2;


        //随机位置
        var rand = new Random();
        x = rand.nextInt(m, srcw - logow - m);
        y = rand.nextInt(m, srch - logoh - m);
        //设置添加图标的透明度
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, .5f));
        //将logo画到当前图片上
        g.drawImage(logo, x, y, null);


        g.dispose();
        //保存图像文件
        try {
            ImageIO.write(src, "png", new File("pok222.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        //打开图像文件
        try {
            Runtime.getRuntime().exec("cmd /k start pok222.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
